package bc.swim2.ejb.session;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import bc.swim2.ejb.entity.Ability;
import bc.swim2.ejb.entity.RegisteredUser;

/**
 * Session Bean implementation class UserManager
 */
@Stateless
@Remote(UserManagerBeanRemote.class)
public class UserManagerBean implements UserManagerBeanRemote {

	/**
	 * Default constructor.
	 */
	public UserManagerBean() {
		// TODO Auto-generated constructor stub
	}

	@PersistenceContext(unitName = "swimv2")
	private EntityManager em;

	@SuppressWarnings("unchecked")
	public RegisteredUser validateLogin(String username, String password) {
		List<RegisteredUser> users = null;

		try {
			Query q = em
					.createNamedQuery("RegisteredUser.getRegisteredUserByUsernamePassword");
			q.setParameter("username", username);
			q.setParameter("password", password);
			users = (List<RegisteredUser>) q.getResultList();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		if (users.isEmpty()) {
			return null;
		} else {
			return users.get(0);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RegisteredUser> getUsersByPattern(String name) {
		List<RegisteredUser> users = null;

		try {
			Query q = em
					.createNamedQuery("RegisteredUser.getRegisteredUserByPattern");

			name = "%" + name.replace(" ", "%") + "%";
			q.setParameter("pattern", name);

			users = (List<RegisteredUser>) q.getResultList();

		} catch (Exception e) {
			e.printStackTrace();
		}

		// Now usersByFirst contains all result Users

		return users;
	}

	@Override
	public RegisteredUser getUserById(int userId) {
		RegisteredUser user = null;

		try {
			Query q = em
					.createNamedQuery("RegisteredUser.getRegisteredUserById");
			q.setParameter("id", userId);

			user = (RegisteredUser) q.getResultList().get(0);

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return user;
	}

	@Override
	public void declareAbility(RegisteredUser ru, Ability a) {

		try {
			RegisteredUser reus = em.find(RegisteredUser.class, ru.getId());
			Ability ab = em.find(Ability.class, a.getId());
			reus.getAbilityPossessed().add(ab);
			em.merge(reus);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RegisteredUser> getUsersByAbilityIdAllOrFriends(int abilityId) {
		List<RegisteredUser> users = null;

		try {
			Query q = em
					.createNamedQuery("RegisteredUser.getRegisteredUserByAbilityId");
			q.setParameter("id", abilityId);

			users = (List<RegisteredUser>) q.getResultList();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return users;
	}

	@Override
	public void removeAbility(int userId, int abilityId) {

		try {
			RegisteredUser ru = em.find(RegisteredUser.class, userId);
			Ability a = em.find(Ability.class, abilityId);
			ru.getAbilityPossessed().remove(a);
			em.merge(ru);

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

	}
}
